﻿using System.Security.Principal;
using System.Windows;
using System.Windows.Controls;
using System;
using System.Linq;

namespace SecurePO
{
    /// <summary>
    /// Interaction logic for NewPurchaseOrderItem.xaml
    /// </summary>
    public partial class NewPurchaseOrderItem : UserControl
    {
        public NewPurchaseOrderItem()
        {
            InitializeComponent();
        }

        public string Title
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public string UserName
        {
            get;
            set;
        }

        public decimal Amount
        {
            get;
            set;
        }

        public DateTime Added
        {
            get;
            set;
        }

        public DateTime Approved
        {
            get;
            set;
        }

        public bool CanApprove
        {
            get;
            set;
        }

        public bool BioIdentityRequired
        {
            get;
            set;
        }

        public string ApprovedBy
        {
            get;
            set;
        }

        public bool IsNew
        {
            get;
            set;
        }

        public event EventHandler Cancelled;

        public event EventHandler<NewPurchaseOrderSaveEventArgs> SaveRequested;

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            adNameListBox.ItemsSource = ActiveDirectoryHelper.GetUsers().OrderBy(a => a.Value).Select(a => a.Value).ToArray();
        }

        private void SavePOButton_Click(object sender, RoutedEventArgs e)
        {
            if (SaveRequested != null)
            {
                if (adNameListBox.SelectedIndex == -1)
                {
                    adNameListBox.SelectedIndex = 0;
                }
                string approver = ActiveDirectoryHelper.GetUsers().OrderBy(a => a.Value).ToList()[adNameListBox.SelectedIndex].Key;
                string title = TitleBox.Text;
                string description = DescriptionBox.Text;
                decimal amount;
                decimal.TryParse(AmountBox.Text, out amount);
                PurchaseOrder po = new PurchaseOrder { AddedBy = WindowsIdentity.GetCurrent().User.Value, Approver = approver, Title = title, Description = description, Amount = amount, Approved = DateTime.MaxValue, Added = DateTime.Now, Number = Guid.NewGuid().ToString() };
                SaveRequested(this, new NewPurchaseOrderSaveEventArgs(po));
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (Cancelled != null)
            {
                Cancelled(this, new EventArgs());
            }
        }
    }

    public class NewPurchaseOrderSaveEventArgs : EventArgs
    {
        public NewPurchaseOrderSaveEventArgs(PurchaseOrder po)
        {
            this.PO = po;
        }

        public PurchaseOrder PO
        {
            get;
            private set;
        }
    }
}